<template>
  <el-card class="card">
    <el-row type="flex" justify="space-between" style="margin-bottom: 10px">
      <el-col :span="2">
        <el-button type="primary" @click="add">添加</el-button>
      </el-col>
      <el-col :span="6">
        <div style="display: flex">
          <el-input placeholder="根据名称搜索" v-model="keyword"></el-input>
          <el-button style="margin-left: 10px" @click="search">搜索</el-button>
        </div>
      </el-col>
    </el-row>
    <el-table :data="books" border>
      <el-table-column align="center" prop="name" label="书籍名称" />
      <el-table-column align="center" prop="author" label="书籍作者" />
      <el-table-column align="center" prop="time" label="出版时间" />
      <el-table-column align="center" prop="where" label="出版社" />
      <el-table-column align="center" prop="type" label="书籍类型" />
      <el-table-column align="center" prop="status" label="是否借阅">
        <template slot-scope="{ row }">
          <el-tag :type="row.status ? 'danger' : 'success'" effect="dark">
            {{ row.status ? "已借阅" : "未借阅" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="status" label="操作">
        <template slot-scope="{ row }">
          <span class="actions"><i class="el-icon-edit" @click="update(row)"></i></span>
          <span class="actions"><i class="el-icon-delete"  @click="remove(row)"></i></span>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="visible" title="内容">
      <el-form label-position="left" label-width="100px">
        <el-form-item label="书籍名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="作者">
          <el-input v-model="form.author"></el-input>
        </el-form-item>
        <el-form-item label="出版时间">
          <el-input v-model="form.time"></el-input>
        </el-form-item>
        <el-form-item label="出版社">
          <el-input v-model="form.where"></el-input>
        </el-form-item>
        <el-form-item label="书籍类型">
          <el-input v-model="form.type"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="addHandler">确定</el-button>
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { DB } from "@/utils";
import { books } from "./data.js";
const Books = new DB("books");
console.log(Books.isNull());
if (Books.isNull()) {
  Books.init(books);
}
export default {
  data() {
    return {
      books: Books.getList(),
      visible: false,
      form: {},
      keyword: ''
    };
  },
  watch:{
    visible(val){
      if(!val){
        this.form = {}
      }
    }
  },
  methods: {
    search(){
      this.books = Books.getList(this.keyword);
    },
    cancel() {
      this.visible = false;
    },
    add() {
      this.visible = true;
    },
    addHandler() {
      if(this.form.id){
        Books.update(this.form);
      }else{
        Books.add(this.form);
      }
      this.books = Books.getList();
      this.visible = false;
      this.$message({ message: "操作成功", type: "success" });
    },
    update(row) {
      this.form = row;
      this.visible = true;
    },
    remove(row){
      Books.delete(row.id);
      this.books = Books.getList();
      this.$message({ message: "操作成功", type: "success" });
    }
  },
};
</script>

<style>
.actions {
  font-size: 18px;
  margin: 0 5px;
  cursor: pointer;
}
</style>